﻿// Copyright (c) 2014 Morten Bakkedal
// This code is published under the MIT License.

using System;

using FuncLib;
using FuncLib.Functions;
using FuncLib.Mathematics;

namespace FuncLibSamples.Testing.ComplexFunctions
{
	public partial class ComplexFunction
	{
		public static implicit operator ComplexFunction(double a)
		{
			return new ComplexRectangularFunction(a, 0.0);
		}

		public static implicit operator ComplexFunction(Complex z)
		{
			return new ComplexRectangularFunction(Complex.Re(z), Complex.Im(z));
		}

		public static ComplexFunction operator +(ComplexFunction f, ComplexFunction g)
		{
			throw new NotImplementedException();
		}

		public static ComplexFunction operator *(ComplexFunction f, ComplexFunction g)
		{
			return new MultiplyFunction(f, g);
		}

		public static ComplexFunction operator /(ComplexFunction f, ComplexFunction g)
		{
			throw new NotImplementedException();
		}

		[Serializable]
		private class MultiplyFunction : ComplexFunction
		{
			private ComplexFunction f, g;

			public MultiplyFunction(ComplexFunction f, ComplexFunction g)
			{
				this.f = f;
				this.g = g;
			}

			protected override Complex ComputeValue(IEvaluator evaluator)
			{
				return f.ComputeValue(evaluator) * g.ComputeValue(evaluator);
			}

			protected override ComplexFunction ComputeDerivative(ComplexVariable variable)
			{
				return f.Derivative(variable) * g + f * g.Derivative(variable);
			}

			protected override Function Re()
			{
				return f.Re() * g.Re() - f.Im() * g.Im();
			}
		}
	}
}
